import request from '../http';

// 订单状态枚举
export enum OrderStatus {
  PENDING_PAYMENT = 1,  // 待付款
  PENDING_SHIPMENT = 2, // 待发货
  PENDING_RECEIPT = 3,  // 待收货
  COMPLETED = 4,        // 已完成
  CANCELLED = 5,        // 已取消
  REFUNDING = 6         // 退款中
}

// 支付状态枚举
export enum PaymentStatus {
  UNPAID = 0,  // 未支付
  PAID = 1     // 已支付
}

// 物流状态枚举
export enum ShippingStatus {
  NOT_SHIPPED = 0, // 未发货
  SHIPPED = 1,     // 已发货
  RECEIVED = 2     // 已收货
}

// 支付方式枚举
export enum PaymentMethod {
  WECHAT = 1,    // 微信支付
  ALIPAY = 2,    // 支付宝
  BANK_CARD = 3, // 银行卡
  BALANCE = 4,   // 余额支付
  COD = 5        // 货到付款
}

// 订单接口
export interface Order {
  id: number
  orderNo: string
  userId: number
  orderStatus: OrderStatus
  paymentStatus: PaymentStatus
  paymentMethod: PaymentMethod
  shippingStatus: ShippingStatus
  totalAmount: number
  goodsAmount: number
  shippingFee: number
  discountAmount: number
  consigneeName: string
  consigneePhone: string
  consigneeAddress: string
  consigneePostcode?: string
  deliveryCompany?: string
  deliveryNo?: string
  remark?: string
  paidAt?: string
  shippedAt?: string
  receivedAt?: string
  createdAt: string
  updatedAt: string
}

// 订单项接口
export interface OrderItem {
  id: number
  orderId: number
  productId: number
  skuId: number
  productName: string
  skuName: string
  productImage: string
  price: number
  quantity: number
}

// 订单详情接口
export interface OrderDetail extends Order {
  items: OrderItem[]
  logs?: OrderLog[]
}

// 订单日志接口
export interface OrderLog {
  id: number
  orderId: number
  action: string
  description: string
  operator?: string
  createdAt: string
}

// 订单统计接口
export interface OrderStats {
  totalOrders: number
  pendingPayment: number
  pendingShipment: number
  pendingReceipt: number
  completed: number
  cancelled: number
  refunding: number
  totalAmount: number
  todayOrders: number
  todayAmount: number
}

// 订单查询参数
export interface OrderQueryParams {
  page?: number
  limit?: number
  orderNo?: string
  orderStatus?: OrderStatus
  paymentStatus?: PaymentStatus
  shippingStatus?: ShippingStatus
  consigneeName?: string
  consigneePhone?: string
  startDate?: string
  endDate?: string
}

// 创建订单请求
export interface CreateOrderRequest {
  userId: number
  items: {
    productId: number
    skuId: number
    quantity: number
    price: number
  }[]
  consigneeName: string
  consigneePhone: string
  consigneeAddress: string
  consigneePostcode?: string
  paymentMethod: PaymentMethod
  remark?: string
}

// 更新订单请求
export interface UpdateOrderRequest {
  orderStatus?: OrderStatus
  paymentStatus?: PaymentStatus
  shippingStatus?: ShippingStatus
  consigneeName?: string
  consigneePhone?: string
  consigneeAddress?: string
  consigneePostcode?: string
  deliveryCompany?: string
  deliveryNo?: string
  remark?: string
}

// 支付订单请求
export interface PayOrderRequest {
  paymentMethod: PaymentMethod
  transactionId?: string
}

// 发货请求
export interface ShipOrderRequest {
  deliveryCompany: string
  deliveryNo: string
  remark?: string
  shippedAt?: string
}

// 购物车项接口
export interface CartItem {
  id: number
  userId: number
  productId: number
  skuId: number
  quantity: number
  selected: boolean
  productName: string
  skuName: string
  productImage: string
  price: number
  stockQuantity: number
  createdAt: string
  updatedAt: string
}

// 购物车请求
export interface CartRequest {
  productId: number
  skuId: number
  quantity: number
}

// 更新购物车请求
export interface UpdateCartRequest {
  quantity?: number
  selected?: boolean
}

// 购物车统计
export interface CartStats {
  totalItems: number
  selectedItems: number
  totalAmount: number
  selectedAmount: number
}

// 订单API
export const orderApi = {
  // 获取订单列表
  getOrderList: (params: OrderQueryParams) => {
    return request.get('/api/orders', { params })
  },

  // 获取订单详情
  getOrderDetail: (id: number) => {
    return request.get(`/api/orders/${id}`)
  },

  // 根据订单号获取订单详情
  getOrderByNo: (orderNo: string) => {
    return request.get(`/api/orders/no/${orderNo}`)
  },

  // 创建订单
  createOrder: (data: CreateOrderRequest) => {
    return request.post('/api/orders', data)
  },

  // 更新订单
  updateOrder: (id: number, data: UpdateOrderRequest) => {
    return request.put(`/api/orders/${id}`, data)
  },

  // 删除订单
  deleteOrder: (id: number) => {
    return request.delete(`/api/orders/${id}`)
  },

  // 获取订单统计
  getOrderStats: () => {
    return request.get('/api/orders/stats')
  },

  // 支付订单
  payOrder: (id: number, data: PayOrderRequest) => {
    return request.post(`/api/orders/${id}/pay`, data)
  },

  // 发货
  shipOrder: (id: number, data: ShipOrderRequest) => {
    return request.post(`/api/orders/${id}/ship`, data)
  },

  // 确认收货
  confirmOrder: (id: number) => {
    return request.post(`/api/orders/${id}/confirm`)
  },

  // 取消订单
  cancelOrder: (id: number) => {
    return request.post(`/api/orders/${id}/cancel`)
  }
}

// 购物车查询参数
export interface CartQueryParams {
  page?: number
  limit?: number
  keyword?: string
}

// 购物车API
export const cartApi = {
  // 获取购物车列表
  getCartList: (params?: CartQueryParams) => {
    return request.get('/api/cart', { params })
  },

  // 添加商品到购物车
  addToCart: (data: CartRequest) => {
    return request.post('/api/cart', data)
  },

  // 更新购物车项
  updateCartItem: (id: number, data: UpdateCartRequest) => {
    return request.put(`/api/cart/${id}`, data)
  },

  // 删除购物车项
  deleteCartItem: (id: number) => {
    return request.delete(`/api/cart/${id}`)
  },

  // 批量删除购物车项
  deleteCartItems: (ids: number[]) => {
    return request.delete('/api/cart/batch', { data: { ids } })
  },

  // 清空购物车
  clearCart: () => {
    return request.delete('/api/cart/clear')
  },

  // 获取选中的购物车项
  getSelectedItems: () => {
    return request.get('/api/cart/selected')
  },

  // 全选/取消全选
  selectAll: (selected: boolean) => {
    return request.post('/api/cart/select-all', { selected })
  },

  // 获取购物车统计
  getCartStats: () => {
    return request.get('/api/cart/stats')
  }
}

export default {
  orderApi,
  cartApi
}